/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2012-10-23         luwei
 */

package com.tianci.appstore.ui.widget;


import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

import com.tianci.appstore.R;

public class SkyAppPager extends ViewFlipper {
    private Context mContext = null;
    protected Animation inTopDown = null;
    protected Animation inDownTop = null;
    protected Animation outTopDown = null;
    protected Animation outDownTop = null;
    private AnimListener mAnimListener = null;
    private Direction mDirection = Direction.HORIZONTAL;

    public enum Direction {HORIZONTAL, VERTICAL}

    public interface AnimListener {
        public boolean onAnimStart(int current);

        public boolean onAnimEnd(int current);
    }

    public SkyAppPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        this.mDirection = Direction.VERTICAL;
        init();
    }

    public SkyAppPager(Context context, Direction direction) {
        super(context);
        this.mDirection = direction;
        this.mContext = context;
        init();
    }

    public void setAnimListener(AnimListener listener) {
        this.mAnimListener = listener;
    }

    public Direction getDirection() {
        return mDirection;
    }

    public void init() {
        switch (mDirection) {
            case HORIZONTAL:
                inTopDown = AnimationUtils.loadAnimation(mContext, R.anim.in_rightleft);
                inDownTop = AnimationUtils.loadAnimation(mContext, R.anim.in_leftright);
                outDownTop = AnimationUtils.loadAnimation(mContext,
                        R.anim.out_leftright);
                outTopDown = AnimationUtils.loadAnimation(mContext,
                        R.anim.out_rightleft);
                break;
            case VERTICAL:
                inTopDown = AnimationUtils.loadAnimation(mContext, R.anim.in_downup);
                inDownTop = AnimationUtils.loadAnimation(mContext, R.anim.in_updown);
                outDownTop = AnimationUtils.loadAnimation(mContext,
                        R.anim.out_updown);
                outTopDown = AnimationUtils.loadAnimation(mContext,
                        R.anim.out_downup);
                break;
            default:
                break;
        }
        setFocusableInTouchMode(false);
        setClickable(false);
        setLongClickable(false);
    }

    public void displayNextPage() {
        setOutAnimation(outTopDown);
        setInAnimation(inTopDown);
        outTopDown.setAnimationListener(outListener);
    }

    public void setAnimationDuring(int inTime, int outTime) {
        inTopDown.setDuration(inTime);
        inDownTop.setDuration(inTime);
        outDownTop.setDuration(outTime);
        outTopDown.setDuration(outTime);
    }

    public void displayPrePage() {
        setInAnimation(inDownTop);
        setOutAnimation(outDownTop);
        outDownTop.setAnimationListener(outListener);
    }

    AnimationListener outListener = new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            if (mAnimListener != null) {
                mAnimListener.onAnimStart(getDisplayedChild());
            }
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            if (mAnimListener != null) {
                mAnimListener.onAnimEnd(getDisplayedChild());
            }
        }
    };

    AnimationListener animListener = new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation animation) {
        }
    };

}